/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.polimi.deib.se2.meteocal.business.control;

import it.polimi.deib.se2.meteocal.business.boundary.EventManager;
import it.polimi.deib.se2.meteocal.business.boundary.InvitationManager;
import it.polimi.deib.se2.meteocal.business.boundary.NotificationManager;
import it.polimi.deib.se2.meteocal.business.boundary.UserManager;
import it.polimi.deib.se2.meteocal.business.entity.Event;
import it.polimi.deib.se2.meteocal.business.entity.User;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import org.joda.time.DateTime;

@Singleton
public class EventReschedulingRoutine {
    
    @EJB
    UserManager um;
    @EJB
    EventManager em;
    @EJB
    NotificationManager nm;
    @EJB
    InvitationManager im;
    
    
    public EventReschedulingRoutine(){
    }
    
    @Schedule(second = "0", minute = "0", hour = "*/12", persistent = false)
    public void proposeRescheduling(){
        
        //for che gira tutti gli utenti
        List<User> allUsers = um.getAllUsers();
        for(User u : allUsers){
            //get che prende tutti gli eventi dell'utente nei prossimi 3 giorni
            List <Event> userEvents = em.findAllEventsIn3Days(u);
            //for che gira gli eventi presi
            for(Event event : userEvents){
                //if event location Ã¨ diverso da null
                if(event.getIdLocation()!=null){
                    //se badweatherlevel == weather del giorno dell'evento
                    String weather = Utils.catchWeather(event);
                    int badWeatherLevel = event.getBadWeatherLevel();
                    String bwl = WeatherCodeConverter.convertBadWeatherLevel(badWeatherLevel);
                    //vedere se funziona la equals
                    if(weather.equals(bwl)){
                        //invia notifica all'utente con proposta di reschedling primo giorno di non badWeatherLevel
                        int days=0;
                        int diff = Utils.getDateDiff(event.getStartDate(), TimeUnit.DAYS);
                        String weatherString = event.getIdLocation().getWeather();
                        for(int i=diff; i<=16; i++){
                            String weatherInDay = Utils.extractWeather(weatherString, i);
                            if(!weatherInDay.equals(bwl)){
                                days = i;
                                break;
                            }
                        }
                        
                        //giorno i
                        Date today = new Date();
                        DateTime todayDT = new DateTime(today);
                        Date reschedulingDate = todayDT.plusDays(days).toDate();
                        
                        int day = reschedulingDate.getDate();
                        int month = reschedulingDate.getMonth() + 1;
                        
                        String details = "Event rescheduling - If you want to reschedule on " + day + " / " + month +" the event " + event.getTitle() + ","
                                + "that has a bad weather condition for you (" + bwl + ") click Accept";
                        
                        //se non esiste notifica uguale mandare notifica
                        boolean notExists = nm.findByDetailsAndReferingEvent(event, details);
                        if(notExists){
                            nm.sendNotification(u, event, null, details);
                        }
                    }
                }
            }
        }
    }
}
